<script setup lang="ts">
import { ref } from "vue";
import icon from "@/assets/icons/iconPark-expand-down-one.svg";
import { queryPointsPro, queryPolylinesPro } from "@/api/pipe";
import pointTable from "./pointTable/index.vue";
import lineTable from "./lineTable/index.vue";
import { useLocalStore } from "@/views/pipe-network/store";

const store = useLocalStore();
const props = defineProps({
  viewState: Boolean,
});
const isShowInfoList = ref(false);
const tabFeatures = ref("currentFeatures");
const keyword = ref("");
const pipeType = ref("pipePoint");
const tableData = ref([]);
const pointListRef = ref([]);
const polylineListRef = ref([]);
const currentPage = ref(1);
const total = ref(0);
const pageSize = ref(5);
const isPublish = ref(false);
const { selectedFeature, updatePointAPICount, updateLineAPICount } =
  storeToRefs(store);

const getAllLines = () => {
  queryPolylinesPro({
    pageSize: pageSize.value,
    pageIndex: currentPage.value,
    isPublish: isPublish.value,
    queryVal: keyword.value,
  }).then(({ data, succeeded }: any) => {
    if (succeeded && Array.isArray(data?.items)) {
      polylineListRef.value = data.items.map((item: any, index: number) => {
        if (item.jsonGeom) {
          const { coordinates } = JSON.parse(item.jsonGeom);
          const [lng, lat] = coordinates[0];
          return {
            ...item,
            coordinates: coordinates.map((item: any) => [item[1], item[0]]),
            startPoint: [lat, lng],
            index: index + 1,
          };
        } else {
          return { ...item, index: index + 1 };
        }
      });
      tableData.value = polylineListRef.value;
      total.value = data.totalCount;
    }
  });
};
const getAllPoints = () => {
  queryPointsPro({
    pageSize: pageSize.value,
    pageIndex: currentPage.value,
    isPublish: isPublish.value,
    queryVal: keyword.value,
  }).then(({ data, succeeded }: any) => {
    if (succeeded && Array.isArray(data?.items)) {
      pointListRef.value = data.items.map((item: any, index: number) => {
        let latlng = null;
        if (item.jsonGeom) {
          const [lng, lat] = JSON.parse(item.jsonGeom).coordinates;
          latlng = [lat, lng];
        }
        return {
          ...item,
          latlng,
          index: index + 1,
        };
      });
      tableData.value = pointListRef.value;
      total.value = data.totalCount;
    }
  });
};
const handleCurrentChange = (val: number) => {
  currentPage.value = val;
  onTabChange(pipeType.value);
};
const onTabChange = (key: string) => {
  switch (key) {
    case "pipePoint":
      getAllPoints();
      break;
    case "pipeLine":
      getAllLines();
      break;
  }
};
const search = () => {
  onTabChange(pipeType.value);
};
const viewCurrent = () => {
  isPublish.value = false;
  keyword.value = "";
  onTabChange(pipeType.value);
};
const viewAll = () => {
  isPublish.value = true;
  keyword.value = "";
  onTabChange(pipeType.value);
};

onMounted(() => getAllPoints());
watchEffect(() => {
  isShowInfoList.value = props.viewState;
  if (props.viewState === false) {
    store.setSelectedFeature(null);
  }
});
watchEffect(() => {
  if (selectedFeature.value) {
    isShowInfoList.value = true;
    const { isPublish: flag, pointGisNo, lineGisNo } = selectedFeature.value;
    tabFeatures.value = flag ? "allFeatures" : "currentFeatures";
    isPublish.value = flag;
    if (pointGisNo) {
      pipeType.value = "pipePoint";
      keyword.value = pointGisNo;
    }
    if (lineGisNo) {
      pipeType.value = "pipeLine";
      keyword.value = lineGisNo;
    }
    setTimeout(() => {
      search();
    }, 200);
  }
});
watchEffect(() => {
  if (updatePointAPICount.value > 0) {
    getAllPoints();
  }
  if (updateLineAPICount.value > 0) {
    getAllLines();
  }
});
</script>

<template>
  <div class="info-list" v-if="isShowInfoList">
    <div>
      <div>
        <div class="title">
          <span>管点/管线信息</span>
          <span
            @click="
              isShowInfoList = false;
              store.setSelectedFeature(null);
            "
            >收起</span
          >
        </div>
        <el-radio-group v-model="tabFeatures" class="tab-features">
          <el-radio-button value="currentFeatures" @click="viewCurrent"
            >当前添加</el-radio-button
          >
          <el-radio-button value="allFeatures" @click="viewAll"
            >所有元素</el-radio-button
          >
        </el-radio-group>
        <div class="search">
          <el-input
            v-model="keyword"
            style="width: 400px; margin-right: 5px"
            placeholder="请输入名称/编号查询"
          />
          <el-button type="primary" round style="width: 100px" @click="search"
            >搜索</el-button
          >
        </div>
        <el-tabs v-model="pipeType" class="pipe-tabs" @tab-change="onTabChange">
          <el-tab-pane label="管点" name="pipePoint">
            <pointTable :table-data="tableData" />
            <div class="pager">
              <el-pagination
                :small="true"
                v-model:current-page="currentPage"
                :page-size="pageSize"
                :pager-count="5"
                layout="total, prev, pager, next"
                :total="total"
                @current-change="handleCurrentChange"
              />
            </div>
          </el-tab-pane>
          <el-tab-pane label="管段" name="pipeLine">
            <lineTable :table-data="tableData" />
            <div class="pager">
              <el-pagination
                :small="true"
                v-model:current-page="currentPage"
                :page-size="pageSize"
                :pager-count="5"
                layout="total, prev, pager, next"
                :total="total"
                @current-change="handleCurrentChange"
              />
            </div>
          </el-tab-pane>
        </el-tabs>
      </div>
    </div>
  </div>
  <div
    class="shortButton"
    v-if="!isShowInfoList"
    @click="isShowInfoList = true"
  >
    <img :src="icon" style="transform: rotate(90deg)" />
    <span>展开</span>
  </div>
</template>

<style scoped lang="scss">
.info-list {
  position: absolute;
  top: 130px;
  right: 50px;
  z-index: 1000;
  width: 570px;
  height: auto;
  font-family: Roboto, sans-serif;
  font-size: 14px;
  line-height: 20px;
  color: rgb(16 16 16 / 100%);
  text-align: center;
  background-color: rgb(255 255 255 / 100%);
  border: 1px solid rgb(187 187 187 / 100%);
}

.title {
  height: 40px;
  padding-left: 30px;
  font-size: 20px;

  span:nth-child(1) {
    position: relative;
    left: -190px;
    line-height: 40px;
  }

  span:nth-child(2) {
    position: relative;
    right: -142px;
    font-size: 16px;
    color: blue;
    cursor: pointer;
  }
}

.tab-features {
  width: 100%;
  height: 32px;
  background-color: rgb(255 255 255 / 100%);
  border: 1px solid #32bebd;
  border-radius: 4px;

  label {
    width: 50%;
  }

  :deep(.el-radio-button__inner) {
    width: 100%;
    padding-bottom: 6px;
  }
}

.search {
  margin: 8px 5px 10px 14px;
}

.pipe-tabs {
  :deep(.el-tabs__nav-scroll) {
    padding-left: 18px;
  }
}

.pager {
  float: right;
  padding-right: 10px;
}

.shortButton {
  position: absolute;
  top: 40%;
  right: 10px;
  z-index: 1000;
  width: 19px;
  height: 74px;
  padding-top: 5px;
  font-family: Roboto, sans-serif;
  font-size: 14px;
  line-height: 20px;
  color: rgb(255 255 255 / 100%);
  text-align: center;
  cursor: pointer;
  background-color: rgb(108 108 108 / 49%);
  border: 1px solid rgb(187 187 187 / 100%);
}
</style>
